package com.navi.loaddata.loadhis;

import com.navi.loaddata.core.mysqlEntity.MBisDataD;
import com.navi.loaddata.core.mysqlEntity.MBisUserD;
import com.navi.loaddata.core.repository.impl.GreenPlumRepositoryImpl;
import com.navi.loaddata.core.repository.impl.MySqlRepositoryImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@Transactional("secondaryPlatformTransactionManager")
public class LoadBisUserAndData {

    @Autowired
    private GreenPlumRepositoryImpl gpdao;
    @Autowired
    private MySqlRepositoryImpl mydao;



    public void syncUserAndData() {
        syncUser();
//        syncData();
    }

    private void syncUser() {
        //清空User表
        gpdao.createNativeQuery(com.navi.loaddata.core.gpEntity.MBisUserD.class, "TRUNCATE TABLE m_bis_user_d").executeUpdate();
        //取出MySQL数据
        try {
            List<MBisUserD> bisUserList = Optional.ofNullable(mydao.find(MBisUserD.class, "FROM MBisUserD")).orElseGet(ArrayList::new);
            bisUserList.forEach(bisUserD -> {
                com.navi.loaddata.core.gpEntity.MBisUserD mBisUserD = new com.navi.loaddata.core.gpEntity.MBisUserD();
                BeanUtils.copyProperties(bisUserD,mBisUserD);
                gpdao.save(mBisUserD);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void syncData() {
        //清空Data表
        gpdao.createNativeQuery(com.navi.loaddata.core.gpEntity.MBisDataD.class, "TRUNCATE TABLE m_bis_data_d").executeUpdate();
        //取出MySQL数据
        try {
            List<MBisDataD> bisDataList = Optional.ofNullable(mydao.find(MBisDataD.class, "FROM MBisDataD")).orElseGet(ArrayList::new);
            bisDataList.forEach(bisDataD -> {
                com.navi.loaddata.core.gpEntity.MBisDataD mBisDataD = new com.navi.loaddata.core.gpEntity.MBisDataD();
                BeanUtils.copyProperties(bisDataD,mBisDataD);
                gpdao.save(mBisDataD);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
